package com.sztx.pay.center.dataaccess.dao.redis.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.stereotype.Repository;

import com.sztx.pay.center.dataaccess.dao.redis.RiskTradeTypeRedisDAO;
import com.sztx.pay.center.dataaccess.domain.RiskTradeTypeDO;
import com.sztx.se.dataaccess.redis.callback.RedisCallback;

@Repository
public class RiskTradeTypeRedisDAOImpl extends AbstractRedisDAOImpl implements RiskTradeTypeRedisDAO {
	
	private static final String RISK_TRADE_TYPE_KEY = "paycenter:risktradetype";
	@Override
	public void saveRiskTradeTypeDO(final List<RiskTradeTypeDO> riskTradeTypeDOs) {
		
        redisTemplate.execute(new RedisCallback<Boolean>() {
            @Override
            public String getKey() {
                return RISK_TRADE_TYPE_KEY;
            }

            @Override
            public Boolean doInRedis(RedisConnection connection, byte[] key) {
                Map<byte[], byte[]> hashes = new HashMap<>();

                for (RiskTradeTypeDO riskTradeTypeDo : riskTradeTypeDOs) {
                    hashes.put(serialize(riskTradeTypeDo.getTradeTypeId()), serialize(riskTradeTypeDo));
                }

                connection.hMSet(key, hashes);
                return Boolean.TRUE;
            }
        });
    }

	@Override
	public void invalidation() {
		invalidation(RISK_TRADE_TYPE_KEY);
	}

	@Override
	public RiskTradeTypeDO getRiskTradeTypeDO(final String tradeTypeId) {
        return redisTemplate.execute(new RedisCallback<RiskTradeTypeDO>() {

            @Override
            public String getKey() {
                return RISK_TRADE_TYPE_KEY;
            }

            @Override
            public RiskTradeTypeDO doInRedis(RedisConnection connection, byte[] key) {
                byte[] bytes = connection.hGet(key, serialize(tradeTypeId));
                if (bytes == null || bytes.length == 0) {
                    return null;
                }

                return deserialize(RiskTradeTypeDO.class, bytes);
            }
        });
    }

}
